package bavaria.hightec.gui;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;

import bavaria.hightec.banking.BankCustomerView;
import bavaria.hightec.banking.Currency;
import bavaria.hightec.banking.Money;
import bavaria.hightec.exceptions.OverCheckException;

public class BankCustomerLayout2 extends JFrame {
	JFrame frameAccountNumber = new JFrame("LogIn");
	JFrame frameFundBalance = new JFrame("Menu");
	JFrame frameFund = new JFrame("Fund");
	JFrame frameFundOk = new JFrame("Complete");
	JFrame frameBalanceOk = new JFrame("Balance");
	
	private BankCustomerView bank;
	private int accountnumber;
	private Button okButton;
	private Button confirmButton;
	private Button fundButton;
	private Button balanceButton;
	private Button completeButton;
	private TextField textField;
	private TextField amount;
	private TextField currency;
	private TextField matter;
	private JLabel balanceMessage;
	private JLabel fundMessage;


	public BankCustomerLayout2(BankCustomerView bank) {
		this.bank=bank;
		
		frameAccountNumber.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frameFundBalance.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frameFund.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frameFundOk.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frameBalanceOk.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frameAccountNumber.setLayout(new FlowLayout());
		frameAccountNumber.setLocation(500, 300);
		frameAccountNumber.add(textField = new TextField("Please type in your account number:", 30));
		frameAccountNumber.add(okButton = new Button("OK"));
		okButton.addActionListener( new AccountNumberOkButtonListener());
		frameAccountNumber.pack();
		frameAccountNumber.setVisible(true);
		
		frameFundBalance.setLayout(new FlowLayout());
		frameFundBalance.setLocation(500, 300);
		frameFundBalance.add(fundButton = new Button("FUND"));
		frameFundBalance.add(balanceButton = new Button("BALANCE"));
		fundButton.addActionListener(new FundButtonListener());
		balanceButton.addActionListener(new BalanceButtonListener());
		frameFundBalance.pack();
		
		frameFund.setLayout(new FlowLayout());
		frameFund.setLocation(500, 300);
		frameFund.add(amount= new TextField("amount"));
		frameFund.add(currency= new TextField("currency"));
		frameFund.add(matter=new TextField("matter"));
		frameFund.add(confirmButton=new Button("CONFIRM"));
		confirmButton.addActionListener(new ConfirmButtonListener());
		frameFund.pack();
		
		frameFundOk.setLayout(new FlowLayout());
		frameFundOk.setLocation(500, 300);
		fundMessage = new JLabel("Transaction done");
        fundMessage.setFont(new Font("Arial", Font.ITALIC, 14));
        frameFundOk.add(fundMessage);
		frameFundOk.add(completeButton = new Button("OK"));
		completeButton.addActionListener(new FundCompleteButtonListener());
        frameFundOk.pack();
		
		frameBalanceOk.setLayout(new FlowLayout());
		frameBalanceOk.setLocation(500, 300);
		frameBalanceOk.add(completeButton = new Button("ok"));
		completeButton.addActionListener(new BalanceCompleteButtonListener());
		frameBalanceOk.pack();
		
	}

	class AccountNumberOkButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			if(bank.accountNumberExists(Integer.valueOf(textField.getText()))){
				accountnumber=Integer.valueOf(textField.getText());
				frameAccountNumber.setVisible(false);
				frameFundBalance.setVisible(true);
			}
			else
				textField.setText("Account number does not exist");
		}
	}
	
	class FundButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0){
			frameFundBalance.setVisible(false);
			frameFund.setVisible(true);
		}
	}
	
	class ConfirmButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			Long geld = Long.valueOf(amount.getText());
			try {
				bank.fund(accountnumber, new Money(geld,Currency.valueOf(currency.getText())), matter.getText());
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (OverCheckException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			frameFund.setVisible(false);
			frameFundOk.setVisible(true);
		}	
	}
	
	class BalanceButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			frameFundBalance.setVisible(false);
			balanceMessage = new JLabel("Balance: "+ bank.balance(accountnumber));
	        balanceMessage.setFont(new Font("Arial", Font.ITALIC, 14));
			frameBalanceOk.add(balanceMessage);
			frameBalanceOk.pack();
			frameBalanceOk.setVisible(true);
			
		}
	}
	
	class BalanceCompleteButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			frameBalanceOk.setVisible(false);
			frameBalanceOk.remove(balanceMessage);
			frameFundBalance.setVisible(true);
		}
		
	}
	
	class FundCompleteButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			frameFundOk.setVisible(false);
			frameFundBalance.setVisible(true);
		}
		
	}

}